package com.ar4j.bench.ar4j.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ar4j.bench.ar4j.domain.Item;
import com.ar4j.bench.common.domain.IItem;
import com.ar4j.bench.common.service.IItemService;
import com.ar4j.spring.AutowiredAdapter;
import com.ar4j.spring.SpringActiveRecordAdapter;

/**
 * An item service implemented using the AR4J library
 */
@Service
public class ItemAr4jService implements IItemService {
  
  @AutowiredAdapter
  private SpringActiveRecordAdapter<Item> itemAdapter;

  @Override
  public Long addItem(IItem item) {
    Item theItem = (Item) item;
    theItem.save();
    return theItem.getId();
  }

  @Override
  public IItem findById(Long id) {
    return itemAdapter.readOnly().findByIdentifier(id);
  }

  @Override
  public List<IItem> findByProductId(Long productId) {
    return new ArrayList<IItem>(itemAdapter.readOnly().findAllByProductId(productId));
  }

  @Override
  public void removeById(Long id) {
    itemAdapter.readOnly().deleteByIdentifier(id);    
  }

  @Override
  public void updateItem(IItem item) {
    Item theItem = (Item) item;
    theItem.save();
  }

}
